#include "ScalarField2.h"
#include "OgreBitwise.h"
using namespace FlowSim;
using namespace Ogre;


ScalarField2::ScalarField2(int size):_size(size)
{
  _data = new Real[size*size];
}

ScalarField2::~ScalarField2()
{
  delete _data;
}

Real* ScalarField2::getData()
{
  return _data;
}
/**
 *This static method creates a 2D scalar field. The values of the 2D scalar field are
 *initialized to 0.
 **/
auto_ptr<ScalarField2> ScalarField2::blankField(int size)
{
  auto_ptr<ScalarField2> sf(new ScalarField2(size));
  Real* data = sf->getData();
  for(int i=0; i<size*size;i++)
    {
      data[i] = 0.0;
    }
  return sf;
}
/**
 *This method zeros all data.
 **/
void ScalarField2::zeroField()
{
  for(int i=0; i<_size*_size;i++)
    {
      _data[i] = 0.0;
    }
}

void ScalarField2::blankFieldGpu(const TexturePtr &tex, int size)
{
  HardwarePixelBufferSharedPtr pixelBuffer = tex->getBuffer();
  pixelBuffer->lock(HardwareBuffer::HBL_NORMAL);
  const PixelBox &pixelBox = pixelBuffer->getCurrentLock();
  uint16* pDest = static_cast<uint16*>(pixelBox.data);
  for(int i=0; i<size*size;i++)
    {
      *pDest++ = Bitwise::floatToHalf(0.0);
      *pDest++ = Bitwise::floatToHalf(0.0);
      *pDest++ = Bitwise::floatToHalf(0.0);
      *pDest++ = Bitwise::floatToHalf(0.0);
    }
  pixelBuffer->unlock();
}

